﻿using System;
using System.ComponentModel.DataAnnotations;
using DotNet.Extensions;
using DotNet.Helper;

namespace DotNet.DataAnnotationsExtensions
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class TelPhoneAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null) return ValidationResult.Success;
            var result = ValidateHelper.IsTelPhone(value.ToStringOrEmpty());
            return result ? ValidationResult.Success : new ValidationResult(ErrorMessage?? "请输入正确的座机号码");
        }
    }
}